<?php

/**
 * Description of common
 *
 * @author Gates by 2013-4-4 17:40:24
 * 
 * Info：
 */
class framework_static_common {

    /**
     * 弹出提示信息，并刷新返回上一页
     * @param string $msg
     */
    public static function history_back($msg = '', $url = '') {
        header("Content-type: text/html; charset=utf-8");
        if ($msg) {
            $msg = "alert('" . $msg . "');";
        }
        if (self::str_empty($url)) {
            echo "<script>" . $msg . "history.go(-2);</script>";
        } else {
            echo "<script>" . $msg . "location.href = '" . $url . "';</script>";
        }
        die;
    }

    /**
     * 判断字符串是否为空
     * @param type $str
     * @return boolean
     */
    public static function str_empty($str = '') {
        if (!isset($str) || empty($str) || preg_replace('/\s(?=\s)/', '', trim($str)) == '') {
            return TRUE;
        }
        return FALSE;
    }

    /**
     * 统计中文字符串长度
     * @param type $str
     * @return type 计算长度类型，0(默认)表示一个中文算一个字符，1表示一个中文算两个字符
     */
    public static function str_length($str = '', $ar = 0) {
        if (function_exists('mb_strlen')) {
            return mb_strlen($str, 'utf-8');
        } else {
            preg_match_all("/./u", $str, $ar);
            return count($ar[0]);
        }
    }

    public static function setUrl() {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?");
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            unset($params['page']);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        return $url;
    }

    /**
     * 显示分页
     * @param type $pageSize 每页大小
     * @param type $total 数据总数
     * @param type $currentPage 当前页码
     * @return string
     */
    public static function page($pageSize = 10, $total = 0, $currentPage = null) {
        $url = self::setUrl();
        $pages = ceil($total / $pageSize);
        if ($currentPage > $pages) {
            $currentPage = $pages;
        }
        if ($currentPage < 1) {
            $currentPage = 1;
        }
        $pre = $currentPage - 1;
        if ($pre < 1) {
            $pre = 1;
        }
        $next = $currentPage + 1;
        if ($next > $pages) {
            $next = $pages;
        }
        $show = "<ul>";
        $show .= "<li><a href='{$url}&page=1'><<</a></li>";
        if ($currentPage == 1) {
            $show .= "<li class=\"disabled\"><a><</a></li>";
        } else {
            $show .= "<li><a href='{$url}&page={$pre}'><</a></li>";
        }
        for ($i = 1; $i <= $pages; $i++) {
            if ($i > $currentPage + 2 || $i < $currentPage - 2)
                continue;
            if ($i == $currentPage)
                $show .= "<li class=\"disabled\"><a><b>{$i}</b></a></li>";
            else
                $show .= "<li><a href='{$url}&page={$i}'><b>{$i}</b></a></li>";
        }
        if ($currentPage == $pages) {
            $show .= "<li class=\"disabled\"><a>></a></li>";
        } else {
            $show .= "<li><a href='{$url}&page={$next}'>></a></li>";
        }
        $show .= "<li><a href='{$url}&page={$pages}'>>></a></li>";
        $show .= "</ul>";
        return $show;
    }

    public static function unescape($str) {
        $ret = '';
        $len = strlen($str);
        for ($i = 0; $i < $len; $i++) {
            if ($str[$i] == '%' && $str[$i + 1] == 'u') {
                $val = hexdec(substr($str, $i + 2, 4));
                if ($val < 0x7f)
                    $ret .= chr($val);
                else
                if ($val < 0x800)
                    $ret .= chr(0xc0 | ($val >> 6)) .
                            chr(0x80 | ($val & 0x3f));
                else
                    $ret .= chr(0xe0 | ($val >> 12)) .
                            chr(0x80 | (($val >> 6) & 0x3f)) .
                            chr(0x80 | ($val & 0x3f));
                $i += 5;
            } else
            if ($str[$i] == '%') {
                $ret .= urldecode(substr($str, $i, 3));
                $i += 2;
            }
            else
                $ret .= $str[$i];
        }
        return $ret;
    }
    
    
    public static function refreshframe($url = '') {
    	echo "<script>top.location.href = '" . $url . "';</script>";
    }
    
    public static function is_array_has_null($arr = array()){
    	foreach ($arr as $val){
    		if($val === ""){
    			return false;
    		}
    	}
    	return true;
    }
    
    public static function upload_image(
    	$config = array(
			"savePath" => "/files/upload",
	        "maxSize" => 10000, //单位KB
	        "allowFiles" => array(".gif", ".png", ".jpg", ".jpeg", ".bmp")
	    )
	    ){
	    $up = new framework_static_uploader("upfile", $config);
	
	    /**
	     * 得到上传文件所对应的各个参数,数组结构
	     * array(
	     *     "originalName" => "",   //原始文件名
	     *     "name" => "",           //新文件名
	     *     "url" => "",            //返回的地址
	     *     "size" => "",           //文件大小
	     *     "type" => "" ,          //文件类型
	     *     "state" => ""           //上传状态，上传成功时必须返回"SUCCESS"
	     * )
	     */
	    $info = $up->getFileInfo();
	    echo json_encode($info);
    }
    
}
